package org.laizili.solution.leetcode;

/**
 * <a href="https://leetcode.cn/problems/consecutive-numbers-sum/">829. 连续整数求和</a>
 */
public class Problem829 {

    // TODO: 简单数论; 等差数列分析
    private static class Solution {
        public int consecutiveNumbersSum(int n) {
            int cnt = 1;
            for (int i = 2; i * (i - 1) / 2 <= n; i++) {
                int remainder = n % i;
                if ((remainder == 0 && ((i & 1) != 0)) || remainder == (i - 1) * i >> 1) {
                    cnt++;
                }
            }
            return cnt;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution().consecutiveNumbersSum(5));
        System.out.println(new Solution().consecutiveNumbersSum(9));
        System.out.println(new Solution().consecutiveNumbersSum(15));
    }
}
